library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;



entity sram_rw_unit IS
    PORT( 
       clk                : IN   std_logic;

       in_sram_wr_addr     : in   std_logic_vector(17 DOWNTO 0);
       in_sram_wr_data     : in   std_logic_vector(15 DOWNTO 0);
       
       in_sram_rq_addr     : in   std_logic_vector(17 DOWNTO 0);
       
       scheduling_done_read    : in   std_logic;
       scheduling_done_write    : in   std_logic;
       
       start_schedule_read      : out   std_logic;
       start_schedule_write     : out   std_logic;
       
       out_sram_read_addr     : out   std_logic_vector(17 DOWNTO 0);
       out_sram_read_data     : out   std_logic_vector(15 DOWNTO 0);
       out_sram_read_valid    : out   std_logic;
       
       sram_addr     : out   std_logic_vector(17 DOWNTO 0);
       sram_data     : inout std_logic_vector(15 DOWNTO 0);
       sram_wren          : out   std_logic
       
    );
end sram_rw_unit;



architecture Behavioral of sram_rw_unit is
 
  signal read_buffer : std_logic := '0';
 
  TYPE STATE_TYPE IS (
      idle,
      write,
      read
   );
 
   SIGNAL state : STATE_TYPE := idle;
 
    
begin
        

 
process (clk)
begin
  if rising_edge(clk) then
    
    case state is
      
      when idle =>
         read_buffer<='0';
         if scheduling_done_read='1' then
           state <= read; 
         elsif scheduling_done_write='1' then
           state <= write; 
         end if;
         
      when read =>
        read_buffer<='1';
        state <= idle;
    
      when others =>
        state <= idle;
      
    end case;
    
  end if;
end process;

  
  start_schedule_read <= '1' when state = read else '0';
  start_schedule_write <= '1' when state = write else '0';
  
  out_sram_read_addr  <= in_sram_rq_addr;
  out_sram_read_data  <= sram_data;
  out_sram_read_valid <= read_buffer;
  
  sram_addr <= in_sram_wr_addr when state=write else in_sram_rq_addr;
  sram_data <= in_sram_wr_data when state=write else (15 downto 0 => 'Z');
  sram_wren      <= '0' when state=write else '1';


end Behavioral;












